const common = {
    getData: function() {
        var myDate = new Date();
        var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
        var date = myDate.getDate(); //获取当前日(1-31)
        //var day = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
        var hour = myDate.getHours(); //获取当前小时数(0-23)
        var minutes = myDate.getMinutes(); //获取当前分钟数(0-59)
        var seconds = myDate.getSeconds(); //获取当前秒数(0-59)
        return year + '年' + (month < 10 ? '0' + String(month) : String(month)) + '月' + date + '日 ' + (hour < 10 ? '0' + String(hour) : String(hour)) + '时' + (minutes < 10 ? '0' + String(minutes) : String(minutes)) + '分' + (seconds < 10 ? '0' + String(seconds) : String(seconds)) + '秒';
    },
    getSign: function(TimeStamp) {
        //TimeStamp = "1570810901";
        var AppID = "admin";
        var AppSecret = "123456";
        var Version = "1.0";
        //var str = "AppID=" + AppID + "&TimeStamp=" + TimeStamp + "&Version=" + Version;
        var str = AppID + TimeStamp + Version;
        var sha1Pw = CryptoJS.HmacSHA1(str, AppSecret).toString();
        var hexarr = [];
        // for (let i = 0; i < sha1Pw.length; i++) {
        //     var tmp = sha1Pw.slice(i * 2, i * 2 + 2); //截取两位转16进制
        //     var hex = parseInt(("0x" + tmp));
        //     if (hex) hexarr.push(hex);
        // }
        for (let i = 0; i < sha1Pw.length / 2; i++) {
            var tmp = sha1Pw.slice(i * 2, i * 2 + 2);
            var hex = parseInt(("0x" + tmp));
            hexarr.push(hex);
        }
        var base64String = common.arrayBufferToBase64(hexarr);
        sha1Pw = encodeURIComponent(base64String);
        return sha1Pw;
    },
    getTimeStamp: function() {
        var TimeStamp = Math.round((new Date()).getTime() / 1000) + 28800;
        return TimeStamp;
    },
    arrayBufferToBase64: function(buffer) {
        var binary = '';
        var bytes = new Uint8Array(buffer);
        var len = bytes.byteLength;
        for (var i = 0; i < len; i++) {
            binary += String.fromCharCode(bytes[i]);
        }
        return window.btoa(binary);
    },
    ajax: function(method, url, param) {
        var timeStamp = this.getTimeStamp();
        var sign = this.getSign(timeStamp);
        return new Promise(function(resolve, reject) {
            $.ajax({
                method: method,
                url: url,
                data: param,
                headers: {
                    AppID: "admin",
                    Version: "1.0",
                    TimeStamp: timeStamp,
                    Sign: sign
                },
                success: function(data) {
                    resolve(data);
                },
                error: function(error) {
                    reject(error)
                }
            })
        })
    },
    ajaxTwo: function(method, url, param) {
        var timeStamp = this.getTimeStamp();
        var sign = this.getSign(timeStamp);
        return new Promise(function(resolve, reject) {
            $.ajax({
                method: method,
                url: url,
                data: param,
                // headers: {
                //     AppID: "admin",
                //     Version: "1.0",
                //     TimeStamp: timeStamp,
                //     Sign: sign
                // },
                success: function(data) {
                    resolve(data);
                },
                error: function(error) {
                    reject(error)
                }
            })
        })
    },
    getYearMonDayMin(date) {
        if (date != null) {
            var dateList = date.split('T');
            var yearMonDay = dateList[0].split('-')
            var hourMinSec = dateList[1].split(':')
            var mon = yearMonDay[1]
            var day = yearMonDay[2]
            var hour = hourMinSec[0]
            var min = hourMinSec[1]
            return `${mon}/${day} ${hour}:${min}`
        } else {
            return ''
        }
    },
    compare(prop) { //数组对象排序 arr.sort(common.compare('key'))
        return function(obj1, obj2) {
            var val1 = obj1[prop];
            var val2 = obj2[prop];
            if (val1 < val2) {
                return -1;
            } else if (val1 > val2) {
                return 1;
            } else {
                return 0;
            }
        }
    },
    isWeekEnd(date) { //判断是否星期天，是的话返回true
        if ("天一二三四五六".charAt(new  Date(date).getDay()) == "天") {
            return true;
        } else {
            return false;
        }
    },
    getMonDay(date) { //2019-11-20T00:00:00
        var dateStr = date.split('T')[0];
        var dateList = dateStr.split('-');
        return dateList[1] + '/' + dateList[2]
    },
    getYearMonDay(date) { //2019-11-20T00:00:00
        var dateStr = date.split('T')[0];
        return dateStr
    },
    compareYearMonDay(oldDate) { //比较有没有超时  仅限天
        var date = new Date(); //实例一个时间对象；
        var year = date.getFullYear(); //获取系统的年；
        var month = date.getMonth() + 1; //获取系统月份，由于月份是从0开始计算，所以要加1
        var day = date.getDate();
        var nowDate = year + "-" + month + "-" + day + " 00:00:00"
        return ((new Date(nowDate.replace(/-/g, "\/"))) > (new Date(oldDate.replace(/-/g, "\/"))));
    },
    getWorkTime(startTime="8:00"){//今天上班的时间，默认8点开始上班
        var current = new Date();//获取当前时间
        var currentHours = current.getHours()
        var currentMin=current.getMinutes()
        if(currentHours<8){//0:00-8:00
            return 0
        }else if((currentHours>=8&&currentHours<11)||(currentHours==11&currentMin<20)){//正常时间8:00-11.20
            var today = new Date(current.getFullYear()+'/'+(current.getMonth()+1)+'/'+current.getDate()+' '+startTime)//今天度的8:00
            console.log(today)
            var time= parseInt(Math.abs(today.getTime()-current.getTime())/1000/60);//返回当前时间到今天8:00（早上）的时间差(总为版正数,若需要正负数都有将权Math.abs去掉)
            console.log(time)
            return time/660
        }else if((currentHours==11&&currentMin>=20)||(currentHours==12&&currentMin<=20)){//从11：20-12:20
            return 200/660
        }else if((currentHours==12&&currentMin>20)||(currentHours>12&&(currentHours<16)||(currentHours==16&&currentMin<=30))){//11:20-16:30
            var today = new Date(current.getFullYear()+'/'+(current.getMonth()+1)+'/'+current.getDate()+' '+startTime)//今天度的8:30
            var time= parseInt(Math.abs(today.getTime()-current.getTime())/1000/60);//返回当前时间到今天8:00（早上）的时间差(总为版正数,若需要正负数都有将权Math.abs去掉)
            return (time-60)/660
        }else if(currentHours==16&&currentMin>30){//16:30-17:00
            return 450/660
        }else if((currentHours>16&&currentHours<20)||(currentHours==20&&currentMin<=30)){//17:00-20:30
            var today = new Date(current.getFullYear()+'/'+(current.getMonth()+1)+'/'+current.getDate()+' '+startTime)//今天度的8:00
            var time= parseInt(Math.abs(today.getTime()-current.getTime())/1000/60-90);//返回当前时间到今天8:00（早上）的时间差(总为版正数,若需要正负数都有将权Math.abs去掉)
            return time/660
        }else if((currentHours==20&&currentMin>30)||currentHours>20){//20:30之后
            return 660/660
        }
    }
}